<?php
namespace App\Repositories\BLL;
use App\Repositories\BLL\Repository;
use App\Models\Wechat\WechatMenu;
use Illuminate\Support\Facades\DB;

class WechatMenuBLL extends Repository
{		
	public function model()
	{
		return WechatMenu::class;
	}
	
	public function getAll($currentRouteName)
	{
		$result = $this->all()->sortBy('sort');
		if ($result) {
			foreach ($result as &$v) {
				$v->actionButton = $v->getListActionButton($currentRouteName);
			}
			//整理格式
			$pArrays = $result->where('pid','=',0);
			$cArrays = $result->where('pid','<>',0);
			foreach($pArrays as &$p){
				$newArray = [];
				foreach($cArrays as $c){
					if($p->id == $c->pid){
						$newArray[] = $c;
					}
				}
				$p->child=$newArray;
			}
		}
		return $pArrays;
	}

	public function createEntity($attributes)
	{
		$result = $this->create($attributes);
		if ($result) {
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	public function editView($id)
	{
		$user = $this->find($id);
		if ($user) {
			return $user;
		}
		abort(404);
	}

	public function updateEntity($attributes,$id)
	{
		$result = $this->update($attributes,$id);
		if ($result) {
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	public function destroyEntity($id)
	{
		$result = $this->delete($id);
		if ($result) {
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
}
